<?php

namespace app\admin\model\agency;

use app\admin\model\Admin;
use fast\Random;
use think\Model;

/**
 * @property $status 0 待审核 1审核通过 2 拒绝
 * @var string
 */

class AgencyInfo extends Model
{


    

    // 表名
    protected $table = 'agency_info';

    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $dateFormat='Y-m-d H:i:s';

    // 追加属性
    protected $append = [

    ];


    protected static function init()
    {
        parent::init(); //
        self::afterUpdate(function ($row) {
            $changed = $row->data;
            $admin=Admin::get(['uid'=>$changed['uid']]);
            if ($changed['status']==1 && empty($admin)) {
                $agency=AgencyInfo::get(['uid'=>$changed['uid']]);
                $params['uid']=$changed['uid'];
                $params['username']=$changed['mobile'];
                $params['nickname']=$agency->city.'总代理';
                $params['mobile']=$changed['mobile'];
                $params['salt'] = Random::alnum();
                $params['password'] = \app\common\library\Auth::instance()->getEncryptPassword(strtolower($params['salt']), $params['salt']);
                $params['avatar'] = '/assets/img/avatar.png';
                Admin::create($params);
            }
        });
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
